<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="会员ID" prop="memberId">
          <el-input
            v-model="queryParams.memberId"
            placeholder="会员ID"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="员工ID" prop="staffId">
          <el-input
            v-model="queryParams.staffId"
            placeholder="员工ID"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="访问类型：1-进入 2-离开" prop="accessType">
          <el-input
            v-model="queryParams.accessType"
            placeholder="访问类型：1-进入 2-离开"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="访问时间" prop="accessTime">
          <el-date-picker
            v-model="queryParams.accessTime"
            type="daterange"
            range-separator="~"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          key="memberId"
          label="会员ID"
          prop="memberId"
          min-width="50"
          align="center"
        />
        <el-table-column
          key="staffId"
          label="员工ID"
          prop="staffId"
          min-width="50"
          align="center"
        />
        <el-table-column
          key="accessType"
          label="访问类型"
          prop="accessType"
          min-width="50"
          align="center"
        >
          <template #default="scope">
            <DictLabel v-model="scope.row.accessType" code="access_Type" />
          </template>
        </el-table-column>

        <el-table-column
          key="accessTime"
          label="访问时间"
          prop="accessTime"
          min-width="150"
          align="center"
        />
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "AccessLogs",
  inheritAttrs: false,
});

import AccessLogsAPI, {
  AccessLogsPageVO,
  AccessLogsForm,
  AccessLogsPageQuery,
} from "@/api/gym/access-logs";

const queryFormRef = ref();
const dataFormRef = ref();

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<AccessLogsPageQuery>({
  pageNum: 1,
  pageSize: 50,
});

// 出入日志表格数据
const pageData = ref<AccessLogsPageVO[]>([]);

// 出入日志表单校验规则
const rules = reactive({});

/** 查询出入日志 */
function handleQuery() {
  loading.value = true;
  AccessLogsAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置出入日志查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

onMounted(() => {
  handleQuery();
});
</script>
